package com.myd.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.myd.model.ExtremeData;
import com.myd.service.ExtremeDataService;
import com.myd.util.DownloadExcel;
import com.myd.vo.Result;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.io.*;
import java.net.URLDecoder;
import java.util.List;

@Path("/extreme_data")
@Controller
public class ExtremeDataController {

    private static final Logger logger = LoggerFactory.getLogger(ExtremeDataController.class);
    @Autowired
    private ExtremeDataService extremeDataService;
    @ResponseBody
    @Path("/add")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public Result insertData(ExtremeData extremeData) {
        boolean b = extremeDataService.insertData(extremeData);
        logger.info("打印信息 -- 添加" + b);
        return Result.success();
    }

    @POST
    @Path("/add_excel")
    @Produces(MediaType.APPLICATION_JSON)
    public Result addExcelData(HttpServletRequest request) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
        String line = null;
        StringBuilder sb = new StringBuilder();
        while((line = br.readLine())!=null){
            sb.append(line);
        }

        // 将资料解码
        String reqBody = sb.toString();
        String decode = URLDecoder.decode(reqBody, "UTF-8");
        JSONObject jsonObject = JSON.parseObject(decode);
        Result result = JSONObject.toJavaObject(jsonObject, Result.class);
        List<ExtremeData> list =(List<ExtremeData>) result.getObject();
        boolean b = extremeDataService.insertExcelData(list);
        Result success = Result.success();
        return success;
    }

    @GET
    @Path("/download_excel/{station_no}")
    @Produces(MediaType.APPLICATION_JSON)
    public void downloadExcel(@PathParam("station_no")int station_no,
                               @Context HttpServletRequest request, @Context HttpServletResponse response){
        String filename = station_no+"极端气象年数据.xlsx";

        List<ExtremeData> list = extremeDataService.selectListByStation_no(station_no);

        XSSFWorkbook excel = DownloadExcel.downloadExtremeExcel(filename,list);
        String headStr = null;
        OutputStream outputStream = null;
        try {
            headStr = "attachment; filename=\"" + new String(filename.getBytes("utf-8"), "ISO8859-1" ) + "\"";
            response.setHeader("Content-Disposition",headStr);
            response.setContentType("application/octet-stream");
            outputStream = response.getOutputStream();
            excel.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
